package com.virtualperson.controller;

import com.virtualperson.dto.VideoGenerationRequest;
import com.virtualperson.dto.VideoGenerationResponse;
import com.virtualperson.service.VideoGenerationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Controller
@RequestMapping("/api/video")
@RequiredArgsConstructor
@Tag(name = "视频生成服务", description = "提供图片和音频生成视频的REST API接口")
public class VideoGenerationController {

    private final VideoGenerationService videoGenerationService;

    @PostMapping("/generate")
    @Operation(
        summary = "生成视频",
        description = "根据输入的图片和音频生成视频",
        responses = {
            @ApiResponse(
                responseCode = "200",
                description = "成功提交视频生成任务",
                content = @Content(schema = @Schema(implementation = VideoGenerationResponse.class))
            ),
            @ApiResponse(
                responseCode = "400",
                description = "请求参数错误",
                content = @Content(schema = @Schema(implementation = ErrorResponse.class))
            )
        }
    )
    public ResponseEntity<?> generateVideo(
        @RequestBody VideoGenerationRequest request
    ) {
        log.info("收到视频生成请求: {}", request);
        try {
            VideoGenerationResponse response = videoGenerationService.generateVideo(request);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("视频生成请求处理失败", e);
            return ResponseEntity.badRequest()
                .body(new ErrorResponse("ERROR", e.getMessage()));
        }
    }

    @GetMapping("/status/{taskId}")
    @Operation(
        summary = "获取任务状态",
        description = "查询视频生成任务的状态",
        responses = {
            @ApiResponse(
                responseCode = "200",
                description = "成功获取任务状态",
                content = @Content(schema = @Schema(implementation = VideoGenerationResponse.class))
            ),
            @ApiResponse(
                responseCode = "400",
                description = "请求参数错误",
                content = @Content(schema = @Schema(implementation = ErrorResponse.class))
            )
        }
    )
    public ResponseEntity<?> getTaskStatus(
        @Parameter(description = "任务ID", required = true)
        @PathVariable String taskId
    ) {
        log.info("查询任务状态: {}", taskId);
        try {
            VideoGenerationResponse response = videoGenerationService.getTaskStatus(taskId);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("获取任务状态失败", e);
            return ResponseEntity.badRequest()
                .body(new ErrorResponse("ERROR", e.getMessage()));
        }
    }

    @GetMapping("/player")
    @Operation(
        summary = "视频播放页面",
        description = "打开视频播放器页面"
    )
    public String videoPlayer(
        @RequestParam(required = false) String url,
        @RequestParam(required = false) String title,
        Model model
    ) {
        model.addAttribute("videoUrl", url);
        model.addAttribute("videoTitle", title);
        return "video";
    }

    /**
     * 错误响应对象
     */
    @Data
    @AllArgsConstructor
    @Schema(description = "错误响应")
    private static class ErrorResponse {
        @Schema(description = "错误状态码", example = "ERROR")
        private String status;
        
        @Schema(description = "错误信息", example = "视频生成失败")
        private String message;
    }
} 